package com.study.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.study.service.BookService;
import com.study.service.LendService;
import com.study.service.StudentService;
import com.study.utils.Pager;
import com.study.vo.Book;
import com.study.vo.Lend;
import com.study.vo.Student;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

public class LendAction extends ActionSupport {
    private Lend lend;
    private LendService lendservice;
    private StudentService studentservice;
    private BookService bookservice;
    private String message;
    private int pageNow = 1;
    private int pageSize = 4;

    public Lend getLend() {
        return lend;
    }

    public void setLend(Lend lend) {
        this.lend = lend;
    }

    public LendService getLendservice() {
        return lendservice;
    }

    public void setLendservice(LendService lendservice) {
        this.lendservice = lendservice;
    }

    public StudentService getStudentservice() {
        return studentservice;
    }

    public void setStudentservice(StudentService studentservice) {
        this.studentservice = studentservice;
    }

    public BookService getBookservice() {
        return bookservice;
    }

    public void setBookservice(BookService bookservice) {
        this.bookservice = bookservice;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public int getPageNow() {
        return pageNow;
    }

    public void setPageNow(int pageNow) {
        this.pageNow = pageNow;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public String selectAllLend() throws Exception {
        if (lend.getReaderId() == null || lend.getReaderId().equals("")) {
            this.setMessage("借书证号为空！");
            return SUCCESS;
        } else if (studentservice.selectStudent(lend.getReaderId()) == null) {
            this.setMessage("您输入的借书证号不存在");
            return SUCCESS;
        }
        this.setMessage("");
        List list = lendservice.selectBook(lend.getReaderId(), this.getPageNow(), this.getPageSize());
        Map request = (Map) ActionContext.getContext().get("request");
        Pager page = new Pager(this.getPageNow(), lendservice.selectBookSize(lend.getReaderId()));
        request.put("list", list);
        request.put("page", page);
        request.put("readerId", lend.getReaderId());
        return SUCCESS;
    }

    public String lendBook() throws Exception {
        if (lend.getIsbn() == null || lend.getIsbn().equals("") || bookservice.selectBook(lend.getIsbn()) == null) {
            this.setMessage("请输入正确的ISBN号！");
            List list = lendservice.selectBook(lend.getReaderId(), this.getPageNow(), this.getPageSize());
            Pager page = new Pager(this.getPageNow(), lendservice.selectBookSize(lend.getReaderId()));
            Map request = (Map) ActionContext.getContext().get("request");
            request.put("page", page);
            request.put("readerId", lend.getReaderId());
            request.put("list", list);
            return SUCCESS;
        } else if (lend.getBookId() == null || lend.getBookId().equals("") ||
                lendservice.selectByBookId(lend.getBookId()) != null) {
            this.setMessage("您未输入图书号或输入的图书号已经存在!");
            List list = lendservice.selectBook(lend.getReaderId(), this.getPageNow(), this.getPageSize());
            Pager page = new Pager(this.getPageNow(), lendservice.selectBookSize(lend.getReaderId()));
            Map request = (Map) ActionContext.getContext().get("request");
            request.put("page", page);
            request.put("readerId", lend.getReaderId());
            request.put("list", list);
            return SUCCESS;
        }
        Lend l = new Lend();
        l.setIsbn(lend.getIsbn());
        l.setBookId(lend.getBookId());
        l.setReaderId(lend.getReaderId());
        l.setLtime(new Timestamp(System.currentTimeMillis()));
        Book b = bookservice.selectBook(lend.getIsbn());
        Student stu = studentservice.selectStudent(lend.getReaderId());
        stu.setNum(stu.getNum() + 1);       //学生借书量+1
        b.setSnum(b.getSnum() - 1);          //图书的库存量-1
        lendservice.addLend(l, b, stu);
        this.setMessage("借书成功！");
        List list = lendservice.selectBook(lend.getReaderId(), this.getPageNow(), this.getPageSize());
        Pager page = new Pager(this.getPageNow(), lendservice.selectBookSize(lend.getReaderId()));
        Map request = (Map) ActionContext.getContext().get("request");
        request.put("page", page);
        request.put("readerId", lend.getReaderId());
        request.put("list", list);
        return SUCCESS;
    }

}

